﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using GMS.Utils;


//5&1&a&s&p&x
public partial class SysEnvConfig : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["UserId"] == null)
        //{
        //    Response.Write("<script>alert('登陆已过期，请重新登陆！');top.location.href='" + Request.ApplicationPath + @"/Login.aspx';</script>");
        //    Response.End();
        //}
        if (!IsPostBack)
        {
            this.PagerBind();
            SetAccessRight();
        }
    }

    private void PagerBind()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("SysConfig.xml"));
        if (ds.Tables.Count > 0)
        {
            this.Repeater1.Visible = true;
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = ds.Tables[0].DefaultView;

            objPds.AllowPaging = false;
            
            this.Repeater1.DataSource = objPds;
            this.Repeater1.DataBind();
        }
        else
        {
            this.Repeater1.Visible = false;
        }
    }

    protected void ButtonUpdate_OnClick(object sender, EventArgs e)
    {
        if (Repeater1.Items.Count == 0)
            return;

        //编辑节点
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("SysConfig.xml"));
        foreach (RepeaterItem item in Repeater1.Items)
        {
            //获得节点列表
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes("settings/setting[title='" + (item.FindControl("HiddenField1") as HiddenField).Value + "']");//查找
            XmlNode xmlNode = xmlNodeList.Item(0);
            xmlNode["content"].InnerText = (item.FindControl("TextBox1") as TextBox).Text.Trim();
            xmlNode["tip"].InnerText = (item.FindControl("TextBox2") as TextBox).Text.Trim();
        }
        //更新信息记录
        XmlNode xmlNode2 = xmlDoc.SelectSingleNode("settings/updatelog");
        xmlNode2["lastUpdateTime"].InnerText = DateTime.Now.ToString();
        if(Session["UserId"] != null)
            xmlNode2["lastUpdateUser"].InnerText = Session["UserId"].ToString();
        xmlNode2["lastUpdateIP"].InnerText = Request.UserHostAddress;

        xmlDoc.Save(Server.MapPath("SysConfig.xml"));

    }

    protected void ButtonDefault_OnClick(object sender, EventArgs e)
    {
        XmlDocument xmlDoc1 = new XmlDocument();
        xmlDoc1.Load(Server.MapPath("~/App_Data/SysConfigBak.xml"));

        /*        foreach (RepeaterItem item in Repeater1.Items)
                {
                    //获得节点列表
                    XmlNodeList xmlNodeList = xmlDoc1.SelectNodes("settings/setting[id='" + (item.FindControl("HiddenField1") as HiddenField).Value + "']");//查找
                    XmlNode xmlNode = xmlNodeList.Item(0);
                    xmlNode["content"].InnerText = (item.FindControl("TextBox1") as TextBox).Text.Trim();
                    xmlNode["tip"].InnerText = (item.FindControl("TextBox2") as TextBox).Text.Trim();
                }
         */

        //更新信息记录
        XmlNode xmlNode = xmlDoc1.SelectSingleNode("settings/updatelog");
        xmlNode["lastUpdateTime"].InnerText = DateTime.Now.ToString();
        if(Session["UserId"] != null)
            xmlNode["lastUpdateUser"].InnerText = Session["UserId"].ToString();
        xmlNode["lastUpdateIP"].InnerText = Request.UserHostAddress;
        
        //存盘
        xmlDoc1.Save(Server.MapPath("SysConfig.xml"));
        Response.Redirect("SysEnvConfig.aspx");
    }

    private void DoRight(GMS.Model.User user, int TabSysID)
    {
        int[] right = RoleModuleRightOper.GetRoleModuleRightInt(user, TabSysID);

        foreach (int i in right)
        {
            switch (i)
            {
                case (int)EnumHelper.E_Right.R_SEARCH:
                    //BtnCustomSearch.Visible = true;
                    break;
                case (int)EnumHelper.E_Right.R_INSERT:
                    this.ButtonAdd.Visible = true;
                    break;
                case (int)EnumHelper.E_Right.R_UPDATE:
                    this.ButtonUpdate.Visible = true;
                    this.ButtonDefault.Visible = true;
                    break;
                case (int)EnumHelper.E_Right.R_DELETE:
                    //none
                    break;
                default:
                    break;
            }
        }
    }

    private void SetAccessRight()
    {
        GMS.Model.User user = (GMS.Model.User)Session["_User"];
        if (user == null)
        {
            Response.Write("<script>alert('登陆已过期，请重新登陆！');top.location.href='" + Request.ApplicationPath + "/Login.aspx';</script>");
            return;
        }

        int TabSysID; //模块ID
        if (Request.Params["TabSysID"] != null)
        {
            TabSysID = Int32.Parse(Request.Params["TabSysID"]);
            if (Session["TabSysID"] == null)
            {
                Session.Add("TabSysID", TabSysID);
            }
            else
            {
                Session["TabSysID"] = TabSysID;
            }

            DoRight(user, TabSysID);
        }
        else if (Session["TabSysID"] != null)
        {
            DoRight(user, Int32.Parse(Session["TabSysID"].ToString()));
        }
        else
        {
            this.ButtonAdd.Visible = true;
            this.ButtonUpdate.Visible = true;
            this.ButtonDefault.Visible = true;
        }

    }
}
